package kin.controller.system;


import java.beans.PropertyEditorSupport;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;


import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;






import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;


import java.io.Serializable;
import java.lang.annotation.Inherited;


import kin.annotation.system.Button;
import kin.annotation.system.Module;
import kin.bean.system.Form;
import kin.bean.system.M2O;
import kin.bean.system.MyEnumType;
import kin.bean.system.Result;

import kin.service.system.FormServiceManager;
import kin.system.util.ApplicationContextHelper;
import kin.system.util.IntPropertyEditor;
import kin.system.util.StringUtil;


public abstract class BaseController {
	@Inject
	private FormServiceManager formServiceManager;
private Result result=new Result();

public Result getResult() {
	return result;
}

private String formName;

//private Gson gson=new Gson();


public void setFormName(String formName) {
	this.formName = formName;
}

@Module
@Button(label = "vvv")
public Object export(){
	return null;
	
}

/*@InitBinder

public void initBinder(WebDataBinder binder,final HttpServletRequest request) {
   //System.out.println("init binder =======================");
 
  // binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
   binder.registerCustomEditor(int.class, new IntPropertyEditor());
   binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
   //binder.registerCustomEditor(Number.class, null, new IntPropertyEditor());
   binder.registerCustomEditor(Float.class, new CustomNumberEditor( Float.class, true));
   binder.registerCustomEditor(Double.class, new CustomNumberEditor(
     Double.class, true));
   binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(
     BigInteger.class, true));
   //binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
   //binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
   binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
   binder.registerCustomEditor(MyEnumType.class, new PropertyEditorSupport() {  
       public void setAsText(String value) {  
           try { 
        	   MyEnumType v=Enum.valueOf(MyEnumType.class, MyEnumType.format(Integer.parseInt(value)));
        	 
               setValue(v);  
           } catch(Exception e) {  
               setValue(null);  
           } 
            }
           }
       );
if(null!=this.formName&&""!=this.formName){

	//System.out.println("bs:"+binder.getBindingResult().getFieldValue("path"));
	 // System.out.println("Objectname:"+binder.getObjectName());
//System.out.println("Teg:"+binder.getTarget().getClass().getSimpleName());
//System.out.println("bs:"+binder.getBindingResult().getObjectName());
   LinkedHashMap<String, Object> whe=new LinkedHashMap<String, Object>();
	whe.put("form_name", StringUtil.fristLetterUpperCase(this.formName));
	Form ff;
	  

		try {
			ff = (Form)this.formServiceManager.find( whe, null,null);
	
			for(M2O m2o:ff.getM2os()){
				
				 Class<?> cls=Class.forName(m2o.getFull_name());
				 final String sn=m2o.getTargetEntity();
				if(cls!=null){
					
			binder.registerCustomEditor(cls, new PropertyEditorSupport() {  
		       public void setAsText(String value) {  
		           try {  
		           	System.out.println("in------------:"+value);
		           	Object o=ApplicationContextHelper.getBean(StringUtil.fristLetterLowerCase(sn)+"ServiceManagerIMP");
		           	Object hh = o.getClass().getMethod("load",new Class[] { Serializable.class }).invoke(o, Integer.parseInt(value));
		           // System.out.println("Obj:"+hh);
		           	setValue(hh); 
		           } catch(Exception e) {  
		           	setValue(null); 
		           }  
		       }  
		 
		              
			}
					);
			}
				}
		} catch (Exception e1) {
			
		}
     }
   binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {  
       public void setAsText(String value) {  
           try {  
        	   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        	   //sdf.setLenient(false);
        	   System.out.println("--------Date------");
               setValue(sdf.parse(value));  
           } catch(ParseException e) {  
               setValue(null);  
           }  
       }  
 
       public String getAsText() {  
           return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());  
       }          
 
   }); 
   
   
}*/



public FormServiceManager getFormServiceManager() {
	return formServiceManager;
}



public void setFormServiceManager(FormServiceManager formServiceManager) {
	this.formServiceManager = formServiceManager;
}

  



}
